package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import actions.UserActionFindAll;
import actions.UserActionFindWhere;
import actions.UserActionLogin;
import actions.UserActionModify;
import actions.UserActionModifyTj;
import actions.UserActionModifyTjAjax;
import actions.UserActionRegister;

/**
 * Servlet implementation class UserServlet
 */

//这是人事管理系统最主要的程序，做集中控制，然后跳转

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		
		 //路径的基本信息，有2个需要注意的路径，一个是服务器本地文件地址，一个是web地址，
		System.out.println(request.getContextPath());
		System.out.println(request.getRealPath(""));
		
	
		
		request.setCharacterEncoding("utf-8");
		String id=request.getParameter("id");
		switch (id) {
		case "1":
			new UserActionLogin().execute(request, response);			
			break;
		case "2":
			System.out.println("进入注册代码");
			new UserActionRegister().execute(request, response);
			break;

		case "3":
			new UserActionFindAll().execute(request, response);
			break;
		case "4":
			new UserActionFindWhere().execute(request, response);
			break;
		case "5":
			new UserActionModify().execute(request, response);
			break;
			
		case "6":
			new UserActionModifyTj().execute(request, response);
			break;
		case "66"://这是ajax提交
			System.out.println("**************进入ajax提交");
			new UserActionModifyTjAjax().execute(request, response);
			break;
			
		case "999":
			System.out.println("**************退出本次会话，清除本次session");
			request.getSession().invalidate();	
			//response.sendRedirect("logout.jsp");//不能使用跳到某一个页面上，这样又会产生一个新的session
			
			//解决response返回乱码分2步
			//第1步，是为了告诉对方
			//response.setHeader("Content-type", "text/html;charset=UTF-8"); //此句也下句可选择之一，否则乱码
			//response.setContentType("text/html");//默认utf-8
			//response.setContentType("text/html; charset=utf-8");
			response.setContentType("text/plain; charset=utf-8");
			//第2步，是为了自身字符或字节的编码
			response.setCharacterEncoding("utf-8");
		
			//返回字符流，使用out=response.getWriter()，当然，也可以使用字符流os
			response.getWriter().println("谢谢您的使用，您已经正式退出系统");
			response.getWriter().println("您的session彻底去除,其他用户刷新页面的时候会显示在线人数少1");
			response.getWriter().println("特别注意，不要使用response.sendRedirect,否则又会产生一个新的session");
			response.getWriter().println("另外注意，response返回的时候要2次设置编码，一要告诉别方，二要约束自己");
			break;
		case "1000":
			System.out.println("**************退出本次会话，清除本次session");
			request.getSession().invalidate();	
			response.sendRedirect("logout.jsp");
			break;
		default:
			break;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
